package demo3;

/**
 * Created with IntelliJ IDEA.
 * User: 12629
 * Date: 2021/10/29
 * Time: 11:13
 * Description:
 */
class Money implements Cloneable{
    public double money = 19.9;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Person implements Cloneable{
    public int age = 99;
    public Money m = new Money();

    @Override
    protected Object clone() throws CloneNotSupportedException {
        //return super.clone();
        Person person = (Person) super.clone();
        person.m = (Money) this.m.clone();
        return person;
    }

    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                '}';
    }
}

public class Test {


    public static void main(String[] args) throws CloneNotSupportedException{
        Person person = new Person();

        Person person2 = (Person) person.clone();//受查异常
        System.out.println(person.m.money);
        System.out.println(person2.m.money);

        System.out.println("==================");

        person2.m.money = 999.99;
        System.out.println(person.m.money);
        System.out.println(person2.m.money);

    }

    public static void main1(String[] args) throws CloneNotSupportedException {
        Person person = new Person();
        System.out.println(person);


        Person person2 = (Person) person.clone();//受查异常
        person2.age = 199;
        System.out.println("===================");
        System.out.println(person2);
        System.out.println(person);

    }
}
